const SQL_TABLE = 'location',
SQL_SELECT = 'select latitude, longitude from ' + SQL_TABLE;

exports.upsert = (location, resolve) => {
	resolve = resolve || (() => {});
	global.pool.query(SQL_SELECT, [], (err, rows, fields) => {
		if (err)
			throw err;
		if (rows.length == 0) {
			this.insert(location, resolve)
		} else {
			this.update(location, resolve)
		}
	})
}

exports.insert = (location, resolve) => {
	resolve = resolve || (() => {});
	global.pool.query('insert into ' + SQL_TABLE + '(latitude, longitude, update_time) values(?, ?, ?)', [location.latitude, location.longitude, location.update_time], (err, result) => {
		if (err)
			throw err;
		resolve()
	})
}

exports.update = (location, resolve) => {
	resolve = resolve || (() => {});
	global.pool.query('update ' + SQL_TABLE + ' set latitude=?, longitude=?, update_time=?', [location.latitude, location.longitude, location.update_time], (err, result) => {
		if (err)
			throw err;
		resolve()
	})
}
